//
//  MoewViewController.m
//  HandWork
//
//  Created by 王阳阳 on 16/1/22.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "MoewViewController.h"
#import "FirstModel.h"
#import "CarouselPicture.h"
#import "UIColor+colorChange.h"
#import "ClassifyView.h"
#import "FourCollectionViewCell.h"
#import "CourseDetailViewController.h"
#import "AFNnetWork.h"

@interface MoewViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
{
    NSInteger _page;
}
@property (nonatomic, retain)NSMutableArray *dataArr;//内容collectionView的数组

@property (nonatomic, retain) UICollectionView *collectionview;//内容CollectionView

@end
@implementation MoewViewController

- (void)dealloc
{
    [_dataArr release];
    [_collectionview release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];//VC的背景颜色
    self.title = @"首页";//导航栏名称
    
    //导航栏右侧按钮
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"pinkFlower"] style:UIBarButtonItemStylePlain target:self action:@selector(flower)];
//    
    //数据解析
    [self data];
    
#pragma mark - 热门内容 CollectionView
    //流式布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //设置每个Item的大小
    layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width / 2 - 20,[UIScreen mainScreen].bounds.size.width / 2 +50);
    //行列间距
    layout.minimumInteritemSpacing =0;
    layout.minimumLineSpacing = 10;
    //边界
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //滑动:方向竖向滑动
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //创建CollectionView
    self.collectionview = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    //签订代理
    self.collectionview.delegate= self;
    self.collectionview.dataSource = self;
    //CollectionView上的背景颜色
    self.collectionview.backgroundColor = [UIColor whiteColor];
    //热门内容的CollectionView添加到View上
    [self.view addSubview:self.collectionview];
    //重用池
    [self.collectionview registerClass:[FourCollectionViewCell class] forCellWithReuseIdentifier:@"cocell"];
    //herder 重用池
    
    [_collectionview release];
    [layout release];
    
    [self createUI];//刷新加载
    
}

#pragma mark - collection 必须实现的方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //获取Item
    FourCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cocell" forIndexPath:indexPath];
    //Model
    FirstModel *f = _dataArr[indexPath.item];
    //item上的图片
    [cell.bgphoto sd_setImageWithURL:[NSURL URLWithString:f.host_pic]];
    //item上的标题
    cell.titlelabel.text = f.subject;
    //item上的变化颜色
    cell.bgview.backgroundColor = [UIColor changeColorWithString:f.bg_color];
    //item上的作者
    cell.user_name.text = f.user_name;
    //item上的收藏个数
    cell.collectable.text = f.collect;
    //item上的喜欢个数
    cell.likelabel.text = f.step_count;
    return cell;
    
    
}
#pragma mark - tableview数据解析
- (void)data
{
    NSString *str = [[@"http://m.shougongke.com/index.php?&c=Course&a=newCourseList&cate_id=" stringByAppendingString:self.str] stringByAppendingString:@"&gcate=cate&order=hot&vid=15"];
    //数据处理  内容数据
    [AFNnetWork GETwithURL:str completion:^(id result) {
        NSArray *arr = result[@"data"];
        self.dataArr = [FirstModel modelHandlerWithArray:arr];
        
        [_collectionview reloadData];//刷新
    }];
    
    [self.collectionview.mj_header endRefreshing];
    
}
#pragma mark - 刷新加载
- (void)createUI{
    //刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 1;
        [self data];
    }];
    [header setTitle:@"我刷我刷我刷刷刷" forState:MJRefreshStatePulling];
    [header beginRefreshing];//开始刷新
    self.collectionview.mj_header = header;
    //加载
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        _page +=20;//加载个数
        [self table];
        
    }];
    self.collectionview.mj_footer = footer;
    
}
#pragma mark - 加载的数据
- (void)table
{
    NSString * str = [[@"http://m.shougongke.com/index.php?&c=Course&a=newCourseList&cate_id=1&gcate=cate&" stringByAppendingString:[NSString stringWithFormat:@"last_id=%ld",_page]] stringByAppendingString:@"&order=hot&vid=15"];
    [AFNnetWork GETwithURL:str completion:^(id result) {
        NSArray *arr = result[@"data"];
        NSArray *atemp = [FirstModel modelHandlerWithArray:arr];
        for (FirstModel *f in atemp) {
            [self.dataArr addObject:f];
        }
        [self.collectionview reloadData];
    }];
    [self.collectionview.mj_footer endRefreshing];
}



#pragma mark - collectionView的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CourseDetailViewController *courdetail = [[CourseDetailViewController alloc]init];
    FirstModel *f = _dataArr[indexPath.row];
    courdetail.deid = f.hand_id;//传值用的id
    courdetail.mo = _dataArr[indexPath.item];

    [self.navigationController pushViewController:courdetail animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
